package com.Jiang.controller;

import com.Jiang.pojo.Book;
import com.Jiang.pojo.ShopCar;
import com.Jiang.service.BookService;
import com.Jiang.service.ShopCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Controller
public class ShopCarController {
    @Autowired
    private ShopCarService shopCarService;
    @Autowired
    private BookService bookService;

    //    1.增加购物车
    @RequestMapping("/addItem")
    public String addItem(int bookId, HttpSession httpSession) {
//        System.out.println(book+"++++++"+bookId);
        System.out.println(bookId);
        Book book = bookService.selectBookById(bookId);
        System.out.println(book);
        HashMap map = new HashMap();
        map.put("userId", (String) httpSession.getAttribute("Login"));
        map.put("bookId", book.getBookId());
        map.put("bookName", book.getBookName());
        map.put("num", 1);
        map.put("price", book.getPrice());
        map.put("url", book.getUrl1());
        map.put("checked",0);
        System.out.println(map);
//        shopCarService.addShopCar(map);
//        ShopCar shopCar = new ShopCar("null",(String) httpSession.getAttribute("Login"),book.getBookId(), book.getBookName(), 1,book.getPrice());
        shopCarService.addShopCar(map);
        return "redirect:/book/mainBook";
    }

    //2.商家查询全部购物车
    @RequestMapping("/adminShopCar")
    public String shopCar(Model model) {
        List<ShopCar> cars = shopCarService.queryCars(new HashMap());
        System.out.println(cars);
        model.addAttribute("allCars", cars);  //给前端传入参数
        return "adminShopCar";
    }
//    3.用户查询自己购物车
    @RequestMapping("/shopCar")
    public String userShopCar(Model model,HttpSession httpSession) {
        List<ShopCar> cars = shopCarService.selectShopCarByUserId((String) httpSession.getAttribute("Login"));
        System.out.println(cars);
        model.addAttribute("userCars", cars);  //给前端传入参数
        return "shopCar";
    }
    //    3.用户删除购物车
    @RequestMapping("/deleteShopCar")
    public String deleteShopCar(HttpSession httpSession, int bookId) {
        HashMap map = new HashMap();
        map.put("userId", httpSession.getAttribute("Login"));
        map.put("bookId", bookId);
        System.out.println(map.get("userId") + "+++++++++++++++++++++++++++++" + map.get("bookId"));
        shopCarService.deleteShopCar(map);
        return "redirect:/shopCar";
    }

    //4.用户删除自己购物车中的全部物品
    @RequestMapping("/deleteAll")
    public String deleteAll(HttpSession httpSession) {
        shopCarService.deleteAll((String) httpSession.getAttribute("Login"));
        return "redirect:/shopCar";
    }

    //    5.用户购物车页面减少购物车商品num
    @RequestMapping("/subNum")
    public String subNum(HttpSession httpSession, int bookId) {
        HashMap map = new HashMap();
        map.put("userId", httpSession.getAttribute("Login"));
        map.put("bookId", bookId);
//        System.out.println(map.get("userId") + "++++++++++++++++++++++++++" + map.get("bookId"));
        shopCarService.subNum(map);
        return "redirect:/shopCar";
    }

    //    6.用户购物车页面增加购物车商品num
    @RequestMapping("/addNum")
    public String addNum(HttpSession httpSession, int bookId) {
        HashMap map = new HashMap();
        map.put("userId", httpSession.getAttribute("Login"));
        map.put("bookId", bookId);
//        System.out.println(map.get("userId") + "++++++++++++++++++++++++++" + map.get("bookId"));
        shopCarService.addNum(map);
        return "redirect:/shopCar";
    }
//    7.用户确定购买购物车中的商品
    @RequestMapping("bookCheck")
    public String bookCheck(HttpSession httpSession, int bookId){
        HashMap map = new HashMap();
        map.put("userId", httpSession.getAttribute("Login"));
        map.put("bookId", bookId);
        shopCarService.toCheck(map);
        return "redirect:/shopCar";
    }

}
